package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.execption.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private DishService dishService;


    @Autowired
    private SetmealService setmealService;
    @Override
    public void delete(Long id) {
        //- 根据当前分类的ID，查询该分类下是否存在菜品，如果存在，则提示错误信息
        LambdaQueryWrapper<Dish> wapper = new LambdaQueryWrapper<>();
        wapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(wapper);
        if(count1 > 0){
            //抛个异常
            throw new CustomException("分类下存在菜品，不能够删除...");
        }
        //- 根据当前分类的ID，查询该分类下是否存在套餐，如果存在，则提示错误信息
        LambdaQueryWrapper<Setmeal> wapper2 = new LambdaQueryWrapper<>();
        wapper2.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(wapper2);
        if(count2 > 0){
            //抛个异常
            throw new CustomException("分类下存在套餐，不能够删除...");
        }
        //- 执行正常的删除分类操作
        removeById(id); //分类删除
    }
}
